﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Workflow
{
    public partial class AuthorityAdd : System.Web.UI.Page
    {
        private AuthorityManager objAuthorityManager = new AuthorityManager();
        public List<UserTeamRole> resultList = new List<UserTeamRole>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<UserInfo> userInfoList = objAuthorityManager.GetUserInfo();
                List<Team> teamList = objAuthorityManager.GetTeamInfo();
                List<WorkflowMould> workflowMouldList = objAuthorityManager.GetWorkflowMould();
                ddlDisplayName.DataSource = userInfoList;
                ddlDisplayName.DataTextField = "DisplayName";
                ddlDisplayName.DataValueField = "UserName";
                ddlDisplayName.DataBind();
                ddlTeam.DataSource = teamList;
                ddlTeam.DataTextField = "TeamName";
                ddlTeam.DataValueField = "TeamId";
                ddlTeam.DataBind();
                ddlWorkName.DataSource = workflowMouldList;
                ddlWorkName.DataTextField = "WorkName";
                ddlWorkName.DataValueField = "WorkParentId";
                ddlWorkName.DataBind();
            }
            else
            {
                UserTeamRole user =new UserTeamRole()
                {
                    WorkMouldId=Convert.ToInt32(ddlWorkName.SelectedValue),
                    UserName=ddlDisplayName.SelectedValue
                
                };
                resultList = objAuthorityManager.GetUserTeamRoles(user);
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            UserTeamRole userTeamRole = new UserTeamRole()
            {
                UserName = ddlDisplayName.SelectedValue,
                TeamId = Convert.ToInt32(ddlTeam.SelectedValue),
                RoleId = Convert.ToInt32(ddlTeam.SelectedValue),
                WorkMouldId = Convert.ToInt32(ddlWorkName.SelectedValue)
            };
            try
            {
                objAuthorityManager.InsertUserTeamRoles(userTeamRole);
                string msg = string.Format("<script>alert('添加成功')</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
            catch (Exception ex)
            {
                string msg = string.Format("<script>alert('{0}')</script>", ex.Message);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
        }

        protected void ddlTeam_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<Role> roleList = objAuthorityManager.GetRoleInfo(Convert.ToInt32(ddlTeam.SelectedValue));
            ddlRole.DataSource = roleList;
            ddlRole.DataTextField = "RoleName";
            ddlRole.DataValueField = "RoleId";
            ddlRole.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    }
}